﻿using MySql.Data.MySqlClient;
using System; 
using System.Data;
using System.Data.SqlClient;
using System.Extensions;
using System.Linq;
using System.SystemFramework;
using System.SystemFramework.Models; 

namespace Wfxin.SystemFramework.DAL
{
    public abstract class DBase
    {
        #region COMMAND_TIME
        protected const int COMMAND_TIME_OUT = 60;
        protected const int COMMAND_TIME_OUT_900 = 900;
        protected const int COMMAND_TIME_OUT_1200 = 1200;
        #endregion

      
        #region 所引器
        /// <summary>
        /// 所引器
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <returns></returns>
        protected IDbConnection this[string dbName]
        {
            get
            {
                try
                {
                    ConnectionOptions options = new ConfigManager()["ConnectionStrings"];
                    if (options == null) throw new Exception("获取数据库字符串失败");
                    IDbConnection dbConnection = null;

                    if (dbConnection == null)
                    {
                        switch (options.DataBaseType)
                        {
                            case DataBaseType.SqlServer:
                                dbConnection = SqlClientFactory.Instance.CreateConnection();
                                break;
                            case DataBaseType.MySql:
                                dbConnection = MySqlClientFactory.Instance.CreateConnection();
                                break;
                            default:
                                break;
                        }
                        if (dbConnection != null)
                        {
                            dbConnection.ConnectionString = string.Join(";", options.ConnStr.SplitString(';').Select(item => (item.StartsWith("database") ? $"database={dbName}" : item)));
                        }
                        if (dbConnection.State == ConnectionState.Closed)
                        {
                            dbConnection.Open();
                        }
                    }
                    return dbConnection;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
        protected string GetIdSql()
        {
            ConnectionOptions options = new ConfigManager()["ConnectionStrings"];
            return options.DataBaseType switch
            {
                DataBaseType.SqlServer => "SELECT @@IDENTITY;",
                DataBaseType.MySql => "SELECT LAST_INSERT_ID();",
                DataBaseType.Sqlite => "SELECT LAST_INSERT_ROWID();",
                _ => throw new NotSupportedException("不支持的数据库类型"),
            };
        }
        #endregion


        #region 题库数据库
        /// <summary>
        /// 雨花数据库
        /// </summary>
        protected IDbConnection TikuDBConnection
        {
            get
            {
                return this["tiku"];
            }
        }
        #endregion


        #region EPS本地数据库
        /// <summary>
        /// EPS本地数据库
        /// </summary>
        protected IDbConnection EPSDBConnection
        {
            get
            {
                return this["eps"];
            }
        }

        #endregion


        #region Alex wfpanskxin数据库
        /// <summary>
        /// EPS本地数据库
        /// </summary>
        protected IDbConnection MainDBConnection
        {
            get
            {
                return this["alex"];
            }
        }

        #endregion

    }
}
